<?php
/**
 * @package DBpretty
 * @author WiFeng
 * @link http://521-wf.com
 */

class DBpretty_Options extends Widget_Abstract_Options {
	
	public static function random($length, $numeric = 0) {
		$seed = base_convert(md5(microtime().$_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
		$seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
		$hash = '';
		$max = strlen($seed) - 1;
		for($i = 0; $i < $length; $i++) {
			$hash .= $seed{mt_rand(0, $max)};
		}
		return $hash;
	}
	
	public static function getAllTables() {
		$tables = array();
		$db = Typecho_Db::get();
		if($result = $db->fetchAll($db->query('SHOW TABLES'))) {
			foreach($result as $value) {
				foreach($value as $table) {
					$tables [$table] = $table;
				}
			}
		}
		return $tables;
	}

	public static function transBytes($bytes) {
		$i = 1;
		$k = 1;
		$units = 'B';
		
		while($k < 5) {
			$i *= 1024;
			if($bytes >= $i) {
				switch($k) {
					case 1 : $units = 'KB';break;
					case 2 : $units = 'M';break;
					case 3 : $units = 'G';break;
				}
				$k ++;	
			} else {
				$i /= 1024;
				break;
			}
		}
		return round($bytes / $i, 2).$units;

	}
	
}

?>